package com.itheima.servlet;


import com.itheima.bean.User;
import com.itheima.service.UserService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/userLogin")
public class UserLoginServlet extends HttpServlet {

    private final UserService userService = new UserService();

    @Override//用户登录的servlet
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获得用户输入的信息先转吗
        req.setCharacterEncoding("UTF-8");
        //获得用户输入的用户名和密码
        String name = req.getParameter("name");
        String passWord = req.getParameter("passWord");
        User user = userService.selectUser(name, passWord);
        if(user==null){
            //登录失败转回到登录页面并弹框   回显用户输入的用户名
            req.setAttribute("name",name);
            req.setAttribute("fail",true);
            req.getRequestDispatcher("user-login.jsp").forward(req,resp);
        }else{
            //将查出来的用户信息放入请求区
            req.setAttribute("user",user);
            //转发到登陆成功的页面            这里是学生信息的页面
            req.getRequestDispatcher("selectAllStudent").forward(req,resp);
        }

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}
